"use client"
import { Clock, CheckCircle2, Play } from "lucide-react"
import { cn } from "@/lib/utils"

import type { Todo, Language } from "@/types"

interface TaskDetailsCardProps {
  todos: Todo[]
  currentLang: Language
}

export default function TaskDetailsCard({ todos, currentLang }: TaskDetailsCardProps) {
  const t = currentLang.translations

  const completedTasks = todos.filter((todo) => todo.completed && todo.completedAt)

  const formatDate = (date: Date) => {
    return date.toLocaleDateString(currentLang.code === "zh" ? "zh-CN" : "en-US", {
      month: "short",
      day: "numeric",
      hour: "2-digit",
      minute: "2-digit",
    })
  }

  const formatDuration = (startDate: Date, endDate: Date) => {
    const diffMs = endDate.getTime() - startDate.getTime()
    const diffHours = Math.floor(diffMs / (1000 * 60 * 60))
    const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60))
    const diffDays = Math.floor(diffHours / 24)

    if (diffDays > 0) {
      return currentLang.code === "zh" ? `${diffDays}天 ${diffHours % 24}小时` : `${diffDays}d ${diffHours % 24}h`
    } else if (diffHours > 0) {
      return currentLang.code === "zh" ? `${diffHours}小时 ${diffMinutes}分钟` : `${diffHours}h ${diffMinutes}m`
    } else {
      return currentLang.code === "zh" ? `${diffMinutes}分钟` : `${diffMinutes}m`
    }
  }

  const getDurationColor = (startDate: Date, endDate: Date) => {
    const diffHours = (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60)
    if (diffHours < 1) return "text-green-600"
    if (diffHours < 24) return "text-blue-600"
    if (diffHours < 72) return "text-orange-600"
    return "text-red-600"
  }

  if (completedTasks.length === 0) {
    return (
      <div className="bg-white rounded-lg p-4 text-center text-gray-500">
        <CheckCircle2 className="w-8 h-8 mx-auto mb-2 opacity-50" />
        <p className="text-sm">{currentLang.code === "zh" ? "暂无已完成任务" : "No completed tasks yet"}</p>
      </div>
    )
  }

  return (
    <div className="bg-white rounded-lg p-4 space-y-3 max-h-80 overflow-y-auto custom-scrollbar">
      <div className="flex items-center gap-2 mb-3 pb-2 border-b border-gray-100">
        <CheckCircle2 className="w-4 h-4 text-green-600" />
        <h4 className="font-medium text-gray-800 text-sm">
          {currentLang.code === "zh" ? "任务完成详情" : "Task Completion Details"}
        </h4>
      </div>

      {completedTasks.slice(0, 10).map((todo) => (
        <div key={todo.id} className="border border-gray-100 rounded-lg p-3 space-y-2">
          {/* Task title */}
          <div className="font-medium text-gray-800 text-sm line-through">{todo.text}</div>

          {/* Time details */}
          <div className="grid grid-cols-1 gap-2 text-xs">
            {/* Created time */}
            <div className="flex items-center gap-2 text-gray-600">
              <Play className="w-3 h-3 text-blue-500" />
              <span className="font-medium">{currentLang.code === "zh" ? "创建:" : "Created:"}</span>
              <span>{formatDate(todo.createdAt)}</span>
            </div>

            {/* Completed time */}
            {todo.completedAt && (
              <div className="flex items-center gap-2 text-gray-600">
                <CheckCircle2 className="w-3 h-3 text-green-500" />
                <span className="font-medium">{currentLang.code === "zh" ? "完成:" : "Completed:"}</span>
                <span>{formatDate(todo.completedAt)}</span>
              </div>
            )}

            {/* Duration */}
            {todo.completedAt && (
              <div className="flex items-center gap-2">
                <Clock className="w-3 h-3 text-purple-500" />
                <span className="font-medium text-gray-600">{currentLang.code === "zh" ? "用时:" : "Duration:"}</span>
                <span className={cn("font-medium", getDurationColor(todo.createdAt, todo.completedAt))}>
                  {formatDuration(todo.createdAt, todo.completedAt)}
                </span>
              </div>
            )}
          </div>

          {/* Completion indicator */}
          <div className="flex items-center justify-between pt-2 border-t border-gray-50">
            <div className="flex items-center gap-1">
              <div className="w-2 h-2 bg-green-500 rounded-full"></div>
              <span className="text-xs text-green-600 font-medium">
                {currentLang.code === "zh" ? "已完成" : "Completed"}
              </span>
            </div>
            {todo.completedAt && <span className="text-xs text-gray-400">{todo.completedAt.toLocaleDateString()}</span>}
          </div>
        </div>
      ))}

      {completedTasks.length > 10 && (
        <div className="text-center text-xs text-gray-500 pt-2">
          {currentLang.code === "zh"
            ? `还有 ${completedTasks.length - 10} 个已完成任务...`
            : `+${completedTasks.length - 10} more completed tasks...`}
        </div>
      )}
    </div>
  )
}
